package cn.hxzy.icon.icon.service.impl;

import cn.hxzy.icon.icon.entity.User;
import cn.hxzy.icon.icon.entity.vo.PageBean;
import cn.hxzy.icon.icon.repository.UserRepository;
import cn.hxzy.icon.icon.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 实现
 *
 * @author little black
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    /**
     * 添加对象
     *
     * @param user
     * @return
     */
    @Override
    public void add(User user) {
        userRepository.save(user);
    }

    /**
     * 删除对象
     *
     * @param id
     * @return
     */
    @Override
    public void deleteById(Integer id) {
        userRepository.deleteById(id);
    }

    /**
     * 修改对象
     *
     * @param user
     * @return
     */
    @Override
    public void update(User user) {
        userRepository.save(user);
    }

    /**
     * 查看对象
     *
     * @param pageBean
     * @return
     */
    @Override
    public PageBean<User> findAll(PageBean<User> pageBean) {
        Pageable pageable = PageRequest.of(pageBean.getPage() - 1, pageBean.getSize(), Sort.by(Sort.Order.asc("id")));
        Page<User> all = userRepository.findAll(pageable);
        pageBean.setList(all.getContent());
        pageBean.setCount(all.getTotalElements());
        pageBean.setPageCount(all.getTotalPages());
        return pageBean;
    }

    /**
     * 查询信息->id
     *
     * @param id
     * @return
     */
    @Override
    public User FindById(Integer id) {
        Optional<User> byId = userRepository.findById(id);
        if (byId.isPresent()) {
            return byId.get();
        }
        return null;
    }

    /**
     * 查询name，判断密码是否正确
     *
     * @param loginName
     * @param loginPassword
     * @return
     */
    @Override
    public User findByLoginNameAndLoginPassword(String loginName, String loginPassword) {
        Optional<User> byLoginName = userRepository.findByLoginName(loginName);
        if (byLoginName.isPresent()) {
            User user = byLoginName.get();
            if (user.getLoginPassword().equals(loginPassword)) {
                return user;
            }
        }
        return null;
    }

    /**
     * 模糊查询
     * @param pageBean
     * @return
     */
    @Override
    public PageBean<User> findByNameLike(PageBean<User> pageBean,String name) {
        Pageable pageable = PageRequest.of(pageBean.getPage() - 1, pageBean.getSize(), Sort.by(Sort.Order.asc("id")));
        String s = "%" + name + "%";
        Page<User> byNameLike = userRepository.findByNameLike(s,pageable);
        pageBean.setList(byNameLike.getContent());
        pageBean.setCount(byNameLike.getTotalElements());
        pageBean.setPageCount(byNameLike.getTotalPages());
        return pageBean;
    }
}
